package com.example.demo.utils;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ImageUtil
{

    /**
     * compressPic:图片等比压缩, 并将压缩后的图片输出到指定位置
     * TODO(这里描述这个方法的注意事项 – 可选).
     *
     * @return
     * @author bo.shen
     * @since JDK 1.7
     */
    @Deprecated
    public boolean compressPic(File inputFile, File outputFile, int outputWidth, int outputHeight, boolean proportion)
    {
        try
        {
            // 获得源文件
            if (!inputFile.exists())
            {
                log.error("=======文件不存在=======");
                return false;
            }
            Image img = ImageIO.read(inputFile);
            // 判断图片格式是否正确
            if (img.getWidth(null) == -1)
            {
                log.error("=======图片格式错误=======");
                return false;
            } else
            {
                int newWidth;
                int newHeight;

                // 判断是否是等比缩放
                if (proportion)
                {
                    // 为等比缩放计算输出的图片宽度及高度
                    double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
                    double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
                    // 根据缩放比率大的进行缩放控制
                    double rate = rate1 > rate2 ? rate1 : rate2;
                    newWidth = (int) (img.getWidth(null) / rate);
                    newHeight = (int) (img.getHeight(null) / rate);
                } else
                {
                    newWidth = outputWidth; // 输出的图片宽度
                    newHeight = outputHeight; // 输出的图片高度
                }

                BufferedImage tagIMG = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
                tagIMG.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);

                FileOutputStream out = new FileOutputStream(outputFile);
                // JPEGImageEncoder可适用于其他图片类型的转换
                //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                //encoder.encode(tagIMG);
                out.close();
            }
        } catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    /**
     * compressPic: 获取压缩后的图片二进制流
     *
     * @param inputFile
     * @param width
     * @param height
     * @param gp
     * @return
     * @author bo.shen
     * @since JDK 1.7
     */
    public Map<String, Object> compressPic(InputStream inputIS, int outputWidth, int outputHeight, boolean proportion, String extName) throws Exception
    {
        try
        {
            Map<String, Object> params = new HashMap<String, Object>();
            Image img = ImageIO.read(inputIS);
            // 判断图片格式是否正确
            if (img.getWidth(null) == -1)
            {
                log.error("=======图片格式错误=======");
                return null;
            } else
            {
                int newWidth;
                int newHeight;

                // 判断是否是等比缩放
                if (proportion)
                {
                    // 为等比缩放计算输出的图片宽度及高度
                    double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
                    double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
                    // 根据缩放比率大的进行缩放控制
                    double rate = rate1 > rate2 ? rate1 : rate2;
                    newWidth = (int) (img.getWidth(null) / rate);
                    newHeight = (int) (img.getHeight(null) / rate);
                } else
                {
                    newWidth = outputWidth; // 输出的图片宽度
                    newHeight = outputHeight; // 输出的图片高度
                }

                BufferedImage tagIMG = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
                tagIMG.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);

                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(tagIMG, extName, os);
                InputStream outIS = new ByteArrayInputStream(os.toByteArray());

                params.put("outis", outIS);
                params.put("size", outIS.available());
                return params;
            }

        } catch (IOException e)
        {
            e.printStackTrace();
            throw e;
        }
    }

}



